package com.sherllinhe.member.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.jwt.JWTUtil;
import com.sherllinhe.common.exception.BusinessException;
import com.sherllinhe.common.exception.BusinessExceptionEnum;
import com.sherllinhe.common.util.JwtUtil;
import com.sherllinhe.common.util.SnowUtil;
import com.sherllinhe.member.domain.Member;
import com.sherllinhe.member.domain.MemberExample;
import com.sherllinhe.member.mapper.MemberMapper;
import com.sherllinhe.member.req.MemberLoginReq;
import com.sherllinhe.member.req.MemberRegisterReq;
import com.sherllinhe.member.req.MemberSendCodeReq;
import com.sherllinhe.member.resp.MemberLoginResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 功能：
 * 作者：Sherllin He
 * 日期：2025/4/25 21:09
 */
@Service
@Slf4j
public class MemberService {
    @Autowired
    private MemberMapper memberMapper;
    public Integer count(){
        return Math.toIntExact(memberMapper.countByExample(null));
    };

    public long register(MemberRegisterReq req){
        String mobile = req.getMobile();

        Member memberDB = selectByMobile(mobile);

        if(ObjUtil.isNotNull(memberDB)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST);
        }
        Member member = new Member();
        member.setId(SnowUtil.getSnowflakeNextId());
        member.setMobile(mobile);
        memberMapper.insert(member);
        return member.getId();
    }

    public void sendCode(MemberSendCodeReq req){
        String mobile = req.getMobile();

        // 查找手机号是否存在
        Member memberDB = selectByMobile(mobile);

        // 手机号不存在，注册
        if(ObjUtil.isNull(memberDB)){
            log.info("手机号不存在，插入一条记录");
            Member member = new Member();
            member.setId(SnowUtil.getSnowflakeNextId());
            member.setMobile(mobile);
            memberMapper.insert(member);
        }else{
            log.info("手机号已存在，无需插入记录");
        }

        // 生成验证码
//        String code = RandomUtil.randomString(4);
        String code = "8888";
        log.info("生成的验证码：{}", code);

        //保存短信记录表
        //对接短信平台发送验证码
    }
    public MemberLoginResp login(MemberLoginReq req){
        String mobile = req.getMobile();
        String code = req.getCode();

        // 查找手机号是否存在
        Member memberDB = selectByMobile(mobile);

        // 手机号不存在，抛出异常
        if(ObjUtil.isNull(memberDB)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXIST);
        }

        // 验证码校验
        if(!"8888".equals(code)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_CODE_ERROR);
        }
        // 登录成功，生成token
        MemberLoginResp resp = BeanUtil.copyProperties(memberDB, MemberLoginResp.class);
        String token = JwtUtil.createToken(resp.getId(),resp.getMobile());
        resp.setToken(token);
        return resp;
    }

    private Member selectByMobile(String mobile) {
        MemberExample example = new MemberExample();
        example.createCriteria().andMobileEqualTo(mobile);
        List<Member> list = memberMapper.selectByExample(example);
        if(CollUtil.isEmpty(list)){
            return null;
        }else{
            return list.get(0);
        }
    }
}
